﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace RTUDataAcquisition.AppCode
{
    public class INIFile
    {
        public static string Path;

        [DllImport("kernel32")]
        private static extern int WritePrivateProfileString(string section,
            string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section,
                 string key, string def, StringBuilder retVal,
            int size, string filePath);

        [DllImport("user32.dll", EntryPoint = "MessageBox")]
        private static extern int MessageBox(
            int hwnd,
            string lpText,
            string lpCaption,
            MB wType
        );

        /// <summary>
        /// MessageBox常量
        /// </summary>
        private enum MB
        {
            MB_ABORTRETRYIGNORE = 2,
            MB_APPLMODAL = 0,
            MB_COMPOSITE = 2,
            MB_DEFAULT_DESKTOP_ONLY = 131072,
            MB_DEFBUTTON1 = 0,
            MB_DEFBUTTON2 = 256,
            MB_DEFBUTTON3 = 512,
            MB_DEFBUTTON4 = 768,
            MB_DEFMASK = 3840,
            MB_HELP = 16384,
            MB_ICONASTERISK = 64,
            MB_ICONERROR = MB_ICONHAND,
            MB_ICONEXCLAMATION = 48,
            MB_ICONHAND = 16,
            MB_ICONINFORMATION = MB_ICONASTERISK,
            MB_ICONMASK = 240,
            MB_ICONQUESTION = 32,
            MB_ICONSTOP = MB_ICONHAND,
            MB_ICONWARNING = MB_ICONEXCLAMATION,
            MB_MISCMASK = 49152,
            MB_MODEMASK = 12288,
            MB_NOFOCUS = 32768,
            MB_OK = 0,
            MB_OKCANCEL = 1,
            MB_PRECOMPOSED = 1,
            MB_RETRYCANCEL = 5,
            MB_RIGHT = 524288,
            MB_RTLREADING = 1048576,
            MB_SERVICE_NOTIFICATION = 2097152,
            MB_SETFOREGROUND = 65536,
            MB_SYSTEMMODAL = 4096,
            MB_TASKMODAL = 8192,
            MB_TOPMOST = 262144,
            MB_TYPEMASK = 15,
            MB_USEGLYPHCHARS = 4,
            MB_USERICON = 128,
            MB_YESNO = 4,
            MB_YESNOCANCEL = 3
        }

        /// <summary>
        /// 向ini文件中写入文本键值对
        /// </summary>
        /// <PARAM name="Section">节名</PARAM>
        /// <PARAM name="Key">键</PARAM>
        /// <PARAM name="Value">值</PARAM>
        public static bool WriteObject(string Section, string Key, object Value)
        {
            string s = Value.ToString();
            int r = WritePrivateProfileString(Section, Key, s, Path);
            if (r == 0)
                MessageBox(0, "写配置文件失败！文件只读？", "错误", MB.MB_SYSTEMMODAL | MB.MB_ICONERROR);
            return r != 0;
        }

        /// <summary>
        /// 从ini文件读取文本
        /// </summary>
        /// <PARAM name="Section">节名</PARAM>
        /// <PARAM name="Key">键</PARAM>
        /// <returns>读取到的值，失败返回空文本</returns>
        public static string ReadString(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp,
                                            255, Path);

            return temp.ToString();
        }

        public static string ReadString(string Section, string Key, string defaultValue)
        {
            string s = ReadString(Section, Key);
            if (string.IsNullOrEmpty(s))
                return defaultValue;

            return s;
        }

        /// <summary>
        /// 向配置文件中写入逻辑值。
        /// </summary>
        /// <param name="Section">节名</param>
        /// <param name="Key">键</param>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static bool WriteBool(string Section, string Key, bool Value)
        {
            int r = WritePrivateProfileString(Section, Key, Value.ToString(), Path);
            if (r == 0)
                MessageBox(0, "写配置文件失败！文件只读？", "错误", MB.MB_SYSTEMMODAL | MB.MB_ICONERROR);
            return r != 0;
        }

        /// <summary>
        /// 从配置文件中读取逻辑值。注意：只能正确读取用“WriteBool”方法写入的值。
        /// </summary>
        /// <param name="Section"></param>
        /// <param name="Key"></param>
        /// <returns></returns>
        public static bool ReadBool(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, Path);
            bool r = false;
            bool.TryParse(temp.ToString(), out r);
            return r;
        }

        public static bool ReadBool(string Section, string Key, bool DefaultValue)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, Path);
            bool r = false;
            if (!bool.TryParse(temp.ToString(), out r))
                return DefaultValue;
            return r;
        }

        /// <summary>
        /// 从配置文件中读取数值。
        /// </summary>
        /// <param name="Section"></param>
        /// <param name="Key"></param>
        /// <param name="defaultValue">当指定的节或键不存在时，返回的默认值。</param>
        /// <returns></returns>
        public static double ReadNumeric(string Section, string Key, double defaultValue)
        {
            double r = defaultValue;
            string s = ReadString(Section, Key);
            if (string.IsNullOrEmpty(s))
                return defaultValue;

            double.TryParse(s, out r);
            return r;
        }
    }
}
